package cn.ac.jaas.isc.vegetable.admin.controller;

import cn.ac.jaas.isc.vegetable.admin.api.dto.SysLogDTO;
import cn.ac.jaas.isc.vegetable.admin.api.entity.SysLog;
import cn.ac.jaas.isc.vegetable.admin.api.vo.PreLogVO;
import cn.ac.jaas.isc.vegetable.admin.service.SysLogService;
import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.excel.annotation.ResponseExcel;
import cn.ac.jaas.isc.vegetable.common.security.annotation.Inner;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 张高昌
 * @date 2022/4/28 18:12
 * @description: 日志表控制层
 */
@RestController
@AllArgsConstructor
@RequestMapping("/log")
@Api(value = "log", tags = "日志管理模块")
public class SysLogController {

    private final SysLogService sysLogService;

    /**
     * 简单分页查询
     *
     * @param page   分页对象
     * @param sysLog 系统日志
     * @return
     */
    @GetMapping("/page")
    public R getLogPage(Page page, SysLogDTO sysLog) {
        return R.ok(sysLogService.getLogByPage(page, sysLog));
    }

    /**
     * 删除日志
     *
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_log_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(sysLogService.removeById(id));
    }

    /**
     * 插入日志
     *
     * @param sysLog 日志实体
     * @return success/false
     */
    @Inner
    @PostMapping("/save")
    public R save(@Valid @RequestBody SysLogDTO sysLog) {
        return R.ok(sysLogService.saveLog(sysLog));
    }

    /**
     * 批量插入前端异常日志
     *
     * @param preLogVoList 日志实体
     * @return success/false
     */
    @PostMapping("/logs")
    public R saveBatchLogs(@RequestBody List<PreLogVO> preLogVoList) {
        return R.ok(sysLogService.saveBatchLogs(preLogVoList));
    }

    /**
     * 导出excel 表格
     *
     * @param sysLog 查询条件
     * @return
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('sys_log_export')")
    public List<SysLog> export(SysLog sysLog) {
        return sysLogService.list(Wrappers.query(sysLog));
    }
}
